﻿#include "winmanager.h"
#include "../widget.h"
#include "../ui/formaddgroup.h"
#include "../ui/formviewrecord.h"
#include "../ui/formrecordcontrol.h"
#include <QtGui>
#include <QToolButton>
#include <QtCore>
#include <QtGlobal>
#include "gloweffect.h"

#include <QResizeEvent>
#include <QDebug>

WinManager::WinManager(QObject *parent) :
    QObject(parent),
    m_mainWidget(NULL),
    m_formgaddgroup(NULL),
    m_formviewrecord(NULL),
    m_formrecordcontrol(NULL)
{
//    m_mainWidget = new Widget();
}

WinManager::~WinManager()
{
    if(m_formrecordcontrol){
        delete m_formrecordcontrol;
        m_formrecordcontrol = NULL;
    }
    if(m_formviewrecord){
        delete m_formviewrecord;
        m_formviewrecord = NULL;
    }
    if(m_formgaddgroup){
        delete m_formgaddgroup;
        m_formgaddgroup = NULL;
    }
//    if(m_mainWidget){
//        delete m_mainWidget;
//        m_mainWidget = NULL;
//    }
}

void WinManager::test()
{
    qDebug()<<"testWinMgr";
}

void WinManager::showMain()
{
    if(!m_mainWidget)
        m_mainWidget = new Widget();

    m_mainWidget->show();
}

void WinManager::showAddGroup()
{
    if(!m_formgaddgroup){
		m_formgaddgroup = new FormAddGroup(getMain()->window());
        connect(m_formgaddgroup,&BaseWidget::onClose,[=]{
            if(m_formgaddgroup){
                delete m_formgaddgroup;
                m_formgaddgroup = NULL;
            }
        });
	}

//    showInMainWidget(m_formgaddgroup);
    m_formgaddgroup->show();
    if(WinMgr.getMain()){
        connect(WinMgr.getMain(),&Widget::sig_resize,[=]{
            if(m_formgaddgroup)
                m_formgaddgroup->resize(WinMgr.getMain()->size());
        });
    }

    if(m_formgaddgroup->isHidden())
    {
        m_formgaddgroup->show();
    }
    m_formgaddgroup->activateWindow();
}

void WinManager::showViewRecord()
{
    if(!m_formviewrecord){
        m_formviewrecord = new FormViewRecord(getMain()->window());
        connect(m_formviewrecord,&BaseWidget::onClose,[=]{
            if(m_formviewrecord){
                delete m_formviewrecord;
            }
            m_formviewrecord = NULL;
        });
    }

//    showInMainWidget(m_formdelaysetting);
    m_formviewrecord->show();
    if(WinMgr.getMain()){
        connect(WinMgr.getMain(),&Widget::sig_resize,[=]{
            if(m_formviewrecord)
                m_formviewrecord->resize(WinMgr.getMain()->size());
        });
    }

    if(m_formviewrecord->isHidden())
    {
        m_formviewrecord->show();
    }
    m_formviewrecord->activateWindow();
}

void WinManager::showRecordControl()
{
    if(!m_formrecordcontrol){
        m_formrecordcontrol = new FormRecordControl(FormRecordControl::RCT_Add,getMain()->window());
        connect(m_formrecordcontrol,&BaseWidget::onClose,[=]{
            if(m_formrecordcontrol){
                delete m_formrecordcontrol;
                m_formrecordcontrol = NULL;
            }
        });
    }

//    showInMainWidget(m_formrecordcontrol);
    m_formrecordcontrol->show();
    if(WinMgr.getMain()){
        connect(WinMgr.getMain(),&Widget::sig_resize,[=]{
            if(m_formrecordcontrol)
                m_formrecordcontrol->resize(WinMgr.getMain()->size());
        });
    }

//    QToolButton *button = m_formdelaysetting->findChildren<QToolButton *>("btnclose_window");
//    qDebug()<<button;

    if(m_formrecordcontrol->isHidden())
    {
        m_formrecordcontrol->show();
    }
    m_formrecordcontrol->activateWindow();
}

Widget *WinManager::getMain()
{
    if(!m_mainWidget)
        m_mainWidget = new Widget();
    return m_mainWidget;
}

FormAddGroup *WinManager::getAddGroup()
{
    if(!m_formgaddgroup)
        m_formgaddgroup = new FormAddGroup();
    return m_formgaddgroup;
}

FormViewRecord *WinManager::getViewRecord()
{
    if(!m_formviewrecord)
        m_formviewrecord = new FormViewRecord();
    return m_formviewrecord;
}

FormRecordControl *WinManager::getRecordControl()
{
    if(!m_formrecordcontrol)
        m_formrecordcontrol = new FormRecordControl();
    return m_formrecordcontrol;
}

void WinManager::showInMainWidget(QWidget *widget)
{
    widget->setWindowModality(Qt::ApplicationModal);
//    widget->setAttribute(Qt::WA_DeleteOnClose);

    //glow effect
//    GlowEffect *effect = new GlowEffect(widget);
//    effect->setRadius(5);
//    effect->setGlowColor(Qt::white);
//    widget->setGraphicsEffect(effect);
    //end

//    if(widget->parent())
//        widget->resize(qobject_cast<QWidget*>(widget->parent())->size());

//    widget->show();
//    widget->resize(qobject_cast<QWidget*>(widget->parent())->size());

//    if(WinMgr.getMain())
//        connect(WinMgr.getMain(),&Widget::sig_resize,[=]{
//            if(WinMgr.getMain())
//            {
//                if(widget)
//                    widget->resize(WinMgr.getMain()->size());
//            }
//        });

//    QPalette palette;
//    palette.setColor(QPalette::Background, QColor(0,0,0,180));
//    widget->setPalette(palette);

//    QToolButton *btnclose = new QToolButton(qobject_cast<QWidget*>(widget->parent()));//widget--problem.
//    btnclose->setAttribute(Qt::WA_DeleteOnClose);
//    btnclose->setObjectName("btnclose_window");
//	btnclose->setAutoRaise(true);
//    btnclose->setFixedSize(QSize(25,25));
//    btnclose->setIconSize(QSize(25,25));
//    btnclose->setIcon(QIcon(":/res/images/closeBtn.png"));
//	btnclose->setStyleSheet(QString("QToolButton#%1{background:transparent;border:transparent;}QToolButton#%1:hover{margin:1px;}").arg(btnclose->objectName()));
//    btnclose->move(widget->width() - btnclose->width(),0);
//    btnclose->show();
//    connect(btnclose,&QToolButton::clicked,[=]{
//        btnclose->deleteLater();
//        //delete btnclose;
//        widget->deleteLater();
//    });

//    if(WinMgr.getMain())
//        connect(WinMgr.getMain(),&Widget::sig_resize,[=]{
//            if(!widget){
//                widget->resize(WinMgr.getMain()->size());
//            }
//        });

//    if(qobject_cast<BaseWidget*>(widget)){
//        connect(qobject_cast<BaseWidget*>(widget),&BaseWidget::onClose,[=]{
//            IsClose = true;
//            if(btnclose)
//                btnclose->close();
//        });
//    }
}
